using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NPC_BeginnerGiude : NPCBase
{
    [SerializeField] private DialogueInitialEntryDataSO[] _dialogueDataSOs;
    private List<PlayerProgress> _playerProgressList = new();
    private bool _isInitialEntry;

    protected override void Start()
    {
        base.Start();
        _isInitialEntry = false;
    }
    private void OnEnable()
    {
        EventCenter.AddEventListener<PlayerProgress>(EventName.PlayerProgress, RecordProgress);
    }
    
    private void OnDisable()
    {
        EventCenter.RemoveEventListener<PlayerProgress>(EventName.PlayerProgress, RecordProgress);
    }
    public override void Interact()
    {
        base.Interact();

        DialogueInitialEntryDataSO currentDialogueDataSO = GetDialogueData();

        EventCenter.TriggerEvent<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, currentDialogueDataSO, () => { });
    }

    /// <summary>
    /// 记录游戏总进度，在完成各种阶段的任务后触发记录
    /// </summary>
    /// <param name="playerProgress"></param>
    private void RecordProgress(PlayerProgress playerProgress)
    {
        if (_playerProgressList.Contains(playerProgress) == false)
            _playerProgressList.Add(playerProgress);
    }

    public DialogueInitialEntryDataSO GetDialogueData()
    {
        for (int i = 0; i < _dialogueDataSOs.Length; i++)
        {
            if (_dialogueDataSOs[i].playerProgress == GetCurrentDialogueProgress())
                return _dialogueDataSOs[i];
        }
        return null;
    }
    private PlayerProgress GetCurrentDialogueProgress()
    {
        if(_isInitialEntry == false)
        {
            _isInitialEntry = true;
            return PlayerProgress.InitialEntry;
        }
        else if (_playerProgressList.Contains(PlayerProgress.KilledAllEnemies) && _playerProgressList.Contains(PlayerProgress.CollectedAllItems))
            return PlayerProgress.CollectedAndKilled;

        else if (_playerProgressList.Contains(PlayerProgress.KilledAllEnemies))
            return PlayerProgress.KilledAllEnemies;

        else if (_playerProgressList.Contains(PlayerProgress.CollectedAllItems))
            return PlayerProgress.CollectedAllItems;

        return PlayerProgress.Incomplete;
    }
}
